<?php  
	require_once('conn.php');
	$page = isset($_GET["page"]) ? $_GET["page"] : "1";
	$action = isset($_GET["act"]) ? $_GET["act"] : "";
	$key = (isset($_POST["k"]) ? $_POST["k"] : (isset($_GET["k"]) ? $_GET["k"] : ""));
	$sid = isset($_GET["sid"]) ? $_GET["sid"] : "";
	if($action=="delps"){//清空所有的票数
		if(is_numeric($sid)){			    
			$query = mysql_query("delete from xh_ip where sid=".$sid,$conn);
			$query = mysql_query("update xh_title set vcount=0 where sid=".$sid,$conn);		
			$query = mysql_query("update xh_question set ps=0 where sid=".$sid,$conn);
			mysql_close($conn);
			die("<script>alert('票数清空操作成功!');location.href='admin_subject.php?k=".$key."&page=".$page."';</script>");
		}
	}
	else if($action=="del"){
	 	if(is_numeric($sid)){//执行单个删除主题		 		 
			 $query = mysql_query("delete from xh_subject where id=".$sid,$conn);
			 $query = mysql_query("delete from xh_title where sid=".$sid,$conn);
			 $query = mysql_query("delete from xh_question where sid=".$sid,$conn);
			 $query = mysql_query("delete from xh_ip where sid=".$sid,$conn);
			 $query = mysql_query("delete from xh_userinfo where sid=".$sid,$conn);
			 mysql_close($conn);
			 deldir("xh_upload/".$sid);
			 die("<script>alert('该主题已被删除!');location.href='admin_subject.php?page=".$page."&k=".$key."&page=".$page."';</script>");
		}
	}else if(isset($_POST["operate"]) && $_POST["operate"]=="delvote"){
		 if(isset($_POST["XHID"]) && is_array($_POST["XHID"]) && count($_POST["XHID"])>0){
			 $ids = implode(",",$_POST["XHID"]);
			 for($i=0;$i<count($_POST["XHID"]);$i++){
				 $sid = $_POST["XHID"][$i];
				 deldir("xh_upload/".$sid);
			 }
			 $query = mysql_query("delete from xh_subject where id in (".$ids.")",$conn);		 
			 $query = mysql_query("delete from xh_title where sid in (".$ids.")",$conn);		 
			 $query = mysql_query("delete from xh_question where sid in (".$ids.")",$conn);
			 $query = mysql_query("delete from xh_ip where sid in (".$ids.")",$conn);
			 $query = mysql_query("delete from xh_userinfo where sid=".$sid,$conn);
			 mysql_close($conn);
			 die("<script>alert('该主题已被删除!');location.href='admin_subject.php?k=".$key."&page=".$page."';</script>");
		 }else{
			 mysql_close($conn);
			 die("<script>alert('请选择要删除的条目!');history.back(1);</script>");
		 }
	}else if(isset($_POST["operate"]) && $_POST["operate"]=="search"){
		mysql_close($conn);
	 	die("<script>location.href='admin_subject.php?k=".$key."&page=".$page."';</script>");
	}
?>
<html>
<head>
<?php
  head();
  check();
?>
<title>后台管理中心</title>
</head>
<script language="javascript" type="text/javascript">
function XH() {
	for (var i=0;i<document.XHH.XHID.length;i++) {
		var e=document.XHH.XHID[i];
		e.checked=!e.checked;
	}
}
function frmsubmit(value){
	document.getElementById("operate").value=value;
	document.getElementById("XHH").submit();
}
function del(value){
	if(confirm('确定删除?')){
		location.href=value;
	}
}
</script>
<body>
<table border="0" cellpadding="1" cellspacing="2" width="100%" id="table1" bgcolor="#E1F0FF">
  <tr>
    <td>
	  <table width="100%" id="table2" cellspacing="1">
	    <tr>
    <td id="TableTitle1">管理投票主题 -&gt;
     <?php
	   if(!empty($key))
	   {
		   echo "搜索主题(".$key.")";
	   }
	   else
	   {
		   echo "所有主题";
	   }
	 ?>
	 </td>
     </tr>
	 </table>
      </td>
  </tr>
  <tr>
    <td>
	   <table border="1" width="100%" id="table2" bordercolor="#A9C9E2" cellspacing="1">
        <form action="?act=save&page=<?php echo $page;?>&k=<?php echo $key;?>" method="post" name="XHH" id="XHH">
          <tr id="TableTitle2">
            <td width="5%" align="center">ID</td>
            <td width="5%" align="center"><a href="javascript:XH()">选择</a></td>
            <td align="center">投票主题名称</td>
            <td align="center" width="18%"> 投票主题到期时间</td>
            <td width="10%" align="center"> 调用代码</td>
            <td width="10%" align="center"> 投票结果</td>
            <td width="5%" align="center">状态</td>
            <td width="20%" align="center">单项操作</td>
          </tr>
		  <?php		    
			$selectcmd = "select * from xh_subject ";			
			if(!empty($key) || $key=="0"){
				$selectcmd.= " where subject like '%".$key."%'";				
			}
			$query= mysql_query($selectcmd,$conn);
			$recordcount = mysql_num_rows($query);//记录数
			$pagesize=10;
			$curpage=$page;			
			$pages=10;
			pager($recordcount,$pagesize,$curpage,$pages,$key,"admin_subject.php");
			$selectcmd.=" limit ".$firstcount.",".$pagesize;
			$query=mysql_query($selectcmd,$conn);
			while($row=mysql_fetch_array($query)){				
		  ?>
          <tr id="TableTitle3" onmouseover='this.className="over_TableTitle3"' onmouseout='this.className="out_TableTitle3"' class="out_TableTitle3">
            <td width="5%" align="center"><?php echo $row["id"]; ?></td>
            <td width="5%" align="center"><input type="checkbox" name="XHID[]" id="XHID" value="<?php echo $row["id"]; ?>"></td>
            <td align="center"><a href="admin_subject_modify.php?sid=<?php echo $row["id"];?>"><?php echo $row["subject"];?></a></td>
            <td align="center" width="18%"><?php echo $row["qx"];?></td>
            <td width="10%" align="center"><a href="#" onClick="javascript:window.open('vcode.php?sid=<?php echo $row["id"];?>','','width=300,height=60,top=300,left=350,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no');">查看</a></td>
            <td width="10%" align="center"><a href="view.php?sid=<?php echo $row["id"];?>" target="_blank">查看</a></td>
            <td width="5%" align="center"><?php if(date("Y-m-d H:i:s",mkDate($row["qx"]))>date("Y-m-d H:i:s")){?>正常<?php }else{?><font color='red'>到期</font><?php }?></td>
            <td width="20%" align="center"><a href="admin_subject.php?act=delps&page=<?php echo $curpage;?>&k=<?php echo $key;?>&sid=<?php echo $row["id"]; ?>">清空票数</a>&nbsp;<a href="admin_user.php?sid=<?php echo $row["id"];?>&act=view">查看用户信息</a>&nbsp;<a href="javascript:void(0)" onClick="del('admin_subject.php?act=del&page=<?php echo $curpage?>&k=<?php echo $key?>&sid=<?php echo $row["id"]?>');">删除</a></td>
          </tr>
		  <?php			    
			}
	        mysql_free_result($query);			
		  ?>          
          <tr id="TableTitle2">
            <td width="5%" align="center">ID</td>
            <td width="5%" align="center"><a href="javascript:XH()">选择</a></td>
            <td align="center">投票主题名称</td>
            <td align="center" width="18%"> 投票主题到期时间</td>
            <td width="10%" align="center"> 调用代码</td>
            <td width="10%" align="center"> 投票结果</td>
            <td width="5%" align="center">状态</td>
            <td width="20%" align="center">单项操作</td>
          </tr>
          <tr>
            <td colspan="8"><input class="btn" type="submit" name="delvote" id="delvote" value="删除所选投票主题" onClick="if(confirm('确定删除?')){frmsubmit('delvote');}">
              |
              <input type="text" name="k">
			  <input type="hidden" name="operate" id="operate" value=""/>
              <input class="btn" type="button" name="search" value="搜索投票主题" onClick="frmsubmit('search')"></td>
          </tr>
          <tr>
            <td colspan="8"><?php echo $outhtml;?></td>
          </tr>
        </form>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
<?php mysql_close($conn);?>